package com.example.orderassembly.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.orderassembly.service.IServiceOrderDetailService;
import com.example.orderassembly.service.IServiceOrderService;
import com.woniu.cloud.common.exception.TicketException;
import com.woniuxy.api.ServiceOrderApi;
import com.woniu.cloud.common.dto.Result;
import com.woniuxy.entity.ServiceOrder;
import com.woniuxy.form.GetAfterSaleByPlatform;
import com.woniuxy.form.GetOrder;
import com.woniuxy.form.InsertServiceOrder;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.resync.ResyncReplicationRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 * 用户服务订单表 前端控制器
 * </p>
 *
 * @author yang
 * @since 2021-09-21
 */
@RestController
@Slf4j
@SuppressWarnings("ALL")
public class ServiceOrderController implements ServiceOrderApi {
    @Resource
    IServiceOrderService serviceOrderService;

    /**
     *
     * @param insertServiceOrder
     * @return
     */
    @PostMapping("/insertServiceOrder")
    @Override
    public Result insertServiceOrder(InsertServiceOrder insertServiceOrder) {
        log.info("新增服务订单，同时生成订单详情  删除购物车信息 参数 insertServiceOrder={}",insertServiceOrder);
        if(null==insertServiceOrder){
            log.info("参数错误");
//            return Result.fail("参数错误",null);
            throw  new TicketException("参数异常");
        }
        Boolean bool = serviceOrderService.insertServiceOrder(insertServiceOrder);
        return bool?Result.success("下单成功",bool):Result.fail("失败",bool);
    }

    @Override
    public Result updateServiceOrderStatus(Integer serviceOrderId, Integer status) {
        log.info("修改服务订单状态 0:未付款 1:已付款 2.已取消，3已消费 serviceOrderId={},status={}",serviceOrderId,status);
        if(serviceOrderId==null||status==null){
            log.info("参数错误");
//            return Result.fail("参数错误",null);
            throw  new TicketException("参数异常");
        }
        Boolean bool = serviceOrderService.updateServiceOrderStatus(serviceOrderId,status);
        return bool?Result.success("修改成功",bool):Result.fail("失败",bool);
    }

    @Override
    public Result updateServiceOrderPayType(Integer orderId, Integer payType) {
        Boolean bool = serviceOrderService.updateServiceOrderPayType(orderId,payType);
        return Result.success("修改成功",true);
    }

    @Override
    public Result getServiceOrderById(Integer id) {
        ServiceOrder serviceOrder = serviceOrderService.getServiceOrderById(id);
        return Result.success("查询成功",serviceOrder);
    }

    @Override
    public Result getServiceOrderByPlatform(GetOrder getOrder) {
        log.info("平台分页查看服务订单 参数 getOrder={}",getOrder);
        if(getOrder==null){
            log.info("参数错误");
//            return Result.fail("参数错误",null);
            throw  new TicketException("参数异常");
        }
        IPage<ServiceOrder> iPage = serviceOrderService.getServiceOrderByPlatform(getOrder);
        return iPage.getRecords().size()==0?Result.fail("查询失败",iPage):Result.success("成功",iPage);
    }

    @Override
    public Result getServiceOrderByStore(GetOrder getOrder) {
        log.info("店铺分页查看服务订单 参数 getOrder = {}",getOrder);
        //TODO 通过jwt获取商家ID
        Integer storeId = getOrder.getUserId();
        if(getOrder==null){
            log.info("参数错误");
//            return Result.fail("参数错误",null);
            throw  new TicketException("参数异常");
        }
        IPage<ServiceOrder> iPage = serviceOrderService.getServiceOrderByStore(storeId,getOrder);
        return iPage.getRecords().size()==0?Result.fail("查询失败",iPage):Result.success("成功",iPage);
    }

    @Override
    public Result getServiceOrderByUserId(GetOrder GetOrder) {
        log.info("用户分页查询服务订单 参数 getAfterSaleByPlatform={}",GetOrder);
        if (null==GetOrder){
            log.info("参数错误");
//            return Result.fail("参数错误",null);
            throw  new TicketException("参数异常");
        }
        IPage<ServiceOrder> iPage = serviceOrderService.getServiceOrderByUserId(GetOrder);
        return iPage.getRecords().size()==0?Result.fail("查询失败",iPage):Result.success("成功",iPage);
    }
}

